<template>
  <div>
    <el-tree
      ref="tree"
      check-on-click-node
      draggable
      highlight-current
      :data="list"
      show-checkbox
      node-key="id"
      default-expand-all
      check-strictly
      :default-checked-keys="defaultArr"
      :props="defaultProps"
    />
    <div style="text-align: right">
      <el-button type="primary" @click="onSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import arrayToTree from 'array-to-tree'
import { getPermissionList } from '@/api/permisson'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  name: '',
  props: {
    // eslint-disable-next-line vue/require-default-prop
    roleId: {
      type: String,
      require: true
    }
  },
  data() {
    return {
      defaultArr: [],
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  computed: {},
  created() {
    this.getPermission()
  },
  methods: {
    async getPermission() {
      const res = await getPermissionList()

      this.list = arrayToTree(res.data, {
        parentProperty: 'pid',
        customID: 'id'
      })
      const resRole = await getRoleDetail(this.roleId)
      this.defaultArr = resRole.data.permIds
    },
    closeDialog() {
      this.$emit('close')
    },
    async onSubmit() {
      console.log(this.$refs.tree.getCheckedKeys(), 'tree的所有选中id的集合')
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.closeDialog()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-dialog {
  margin-top: 1vh !important;
}
</style>
